<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/23
 * Time: 13:33
 */

namespace Home\Model;


class FileModel extends \Think\Model{

    protected $_validate =array(
        array('name','require','文件名称必须'),
        array('file_url','require','请上传文件'),
        array('user_id','require','请登录账户'),
        array('name','','文件名称已经存在！',0,'unique',1),

    );

    protected $_auto=array(
        array('create_time','time',1,'function'),
    );

    function all_file($list){
        $file_sql=$this
            ->field('*,dx_file.id as file_id')
            ->join('left join dx_category c on c.id=file_category_id')
            ->where('auditor=%d','1')
            ->order('dx_file.create_time desc')
            ->page($_GET['p'].','.$list)
            ->select();

        foreach ($file_sql as $key=>$val){
            if($val['file_category_id']=='0'){
                $file_sql[$key]['category_name'] ='无';
            }

            $file_sql[$key]['content']=html_entity_decode($val['content']);
            $file_sql[$key]['tag'] =M('file_tag')->where('file_id=%d',$val['file_id'])->select();
        }

        return $file_sql;

    }

    function once_file($id){
        $file_sql =$this
            ->field('*,dx_file.id as file_id')
            ->join('left join dx_category c on c.id=file_category_id')
            ->join('left join dx_user u on user_id=u.id')
            ->where('dx_file.id=%d AND auditor=%d',array($id,'1'))
            ->order('dx_file.create_time desc')

            ->find();

        if($file_sql['file_category_id'] =='0'){
            $file_sql['file_category_id'] ='无';
        }


        $file_sql['tag']=M('file_tag')->where('file_id=%d',$id)->select();
        $file_sql['content'] =html_entity_decode($file_sql['content']);
        return  $file_sql;
    }


}